package com.nstar.helloworld.controller
{
	import org.osflash.arp.ControllerTemplate;
	
	import org.osflash.arp.*;
	import mx.core.Application;
	import com.nstar.base.util.Logger;
	import com.nstar.helloworld.events.QueryEvent;
	import com.nstar.helloworld.commands.QueryCommand;
	import com.nstar.helloworld.events.QueryByIdEvent;
	import com.nstar.helloworld.commands.QueryByIdCommand;

	public class Controller extends ControllerTemplate
	{
		//
		// Properties
		//
		
	    private static var inst:Controller; 	// instance of self
	    public var logger:Logger = Logger.getInstance();
		
		// Inherited: app (Application instance)
	
		////////////////////////////////////////////////////////////////////////////
		//
		// Primitive operations (private)
		//
		////////////////////////////////////////////////////////////////////////////
	 
		////////////////////////////////////////////////////////////////////////////
		// addEventListeners() 注册事件监听
		////////////////////////////////////////////////////////////////////////////
		override protected function addEventListeners ():void
		{
			//
			// Listen for events from the view. Two separate screens may dispatch
			// the same event and these will be handled by the same event handler.
			// No two screens should use the same event for different purposes.
			//
			
			// Flex 2: Instead of using an object reference, use a direct
			// reference to the handleEvent method.
			
			app.queryView.addEventListener ( QueryEvent.QUERY, handleEvent );
			app.queryView.addEventListener ( QueryByIdEvent.QUERY, handleEvent );

		}
		
		////////////////////////////////////////////////////////////////////////////
		// addCommands() 注册事件的执行方法
		////////////////////////////////////////////////////////////////////////////
		override protected function addCommands ():void
		{
			//
			// Note: Commands are added as references to the classes. Allows a 
			// single command to be called from multiple views.
			//
			addCommand ( "queryCommand", QueryCommand ); 
			addCommand ( "queryByIdCommand", QueryByIdCommand ); 
			
		}
		
		////////////////////////////////////////////////////////////////////////////
		//
		// Public methods
		//
		////////////////////////////////////////////////////////////////////////////
		
		////////////////////////////////////////////////////////////////////////////
		// getInstance() 
		////////////////////////////////////////////////////////////////////////////
		public static function getInstance ( appRef:* = null ):Controller
		{
			//
			// Return reference to singleton instance
			//
			if ( !inst )
			{
				// create a single instance of the singleton
				inst = new Controller();
				if ( appRef != undefined )
				{
					// register the application
					inst.registerApp ( appRef );
				}
			}
			return inst;
		}
		
		public function getApp():* {
			return app;
		}		
		
	}
}